<?php

namespace App\Admin\Controllers;

use App\Models\CompanyUser;
use App\Models\ExpressCompany;
use App\Models\Product;
use App\Models\ProductAttrKey;
use App\Models\ProductList;
use App\Models\ProductOrder;
use App\Models\Resume;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use App\Admin\Actions\Post\Restore;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;


class ProductListController extends AdminController
{
    protected $title = '商品清单';

    protected function grid()
    {
        $grid = new Grid(new ProductList());

        $grid->column('id', __('Id号'));
        $grid->column('order_number', __('订单编号'));
        $grid->column('num', __('商品数量'));
        $grid->column('total', __('商品总额'));
        $statusArr = [
            '0' => '待支付',
            '1' => '已支付',
            '10' => '已发货',
            '20' => '已签收'
        ];
        $grid->column('status', __('订单状态'))->using($statusArr);

        $grid->column('uid', __('用户昵称'))->display(function ($uid) {
            return User::where('uid', $uid)->value('nickname');
        });
        $grid->column('create_time', __('创建时间'))->display(function ($create_time) {
            return date('Y-m-d H:i:s', $create_time);
        });
        $grid->column('pay_time', __('支付时间'))->display(function ($pay_time) {
            if ($pay_time > 0) {
                return date('Y-m-d H:i:s', $pay_time);
            } else {
                return '尚未支付';
            }
        });

        $grid->column('send_time', __('发货时间'))->display(function ($send_time) {
            if ($send_time > 0) {
                return date('Y-m-d H:i:s', $send_time);
            } else {
                return '尚未发货';
            }
        });

        $grid->column('finish_time', __('签单时间'))->display(function ($finish_time) {
            if ($finish_time > 0) {
                return date('Y-m-d H:i:s', $finish_time);
            } else {
                return '尚未签单';
            }
        });

        $grid->disableCreateButton();
        $grid->disableColumnSelector();

        $grid->filter(function ($filter) {

            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->where(function ($query) {

                $uidsObj = User::where('nickname', 'like', "%{$this->input}%")->pluck('uid');
                $uidsArr = [];
                if ($uidsObj) {
                    foreach ($uidsObj as $v) {
                        array_unshift($uidsArr, $v);
                    }
                }

                $uids = array_unique($uidsArr);

                $query->whereIn('uid', $uids);

            }, '用户昵称');

        });

        // 全部关闭
        $grid->disableActions();

//        $grid->actions(function ($actions) {
//            // 去掉删除
//            $actions->disableDelete();
//            // 去掉编辑
//            $actions->disableEdit();
//        });

        return $grid;
    }

    protected function form()
    {
        $form = new Form(new ProductList());

        $form->display('product_id', __('商品名称'))->with(function ($product_id) {
            $productName = Product::where('id', $product_id)->value('product_name');
            return $productName;
        });

        $express_arr = ExpressCompany::select('id', 'name')->get();
        $express = [];
        foreach ($express_arr as $val) {
            $express[$val->id] = $val->name;
        }

        $form->select('express_id', __('快递公司'))->required()->options($express);
        $form->text('toolid', __('快递单号'))->required();

        $statusArr = [
            '0' => '待支付',
            '1' => '已支付',
            '10' => '已发货',
            '20' => '已签收'
        ];

        $form->select('status', '状态')->options($statusArr)->required();

        //保存后回调
        $form->saved(function (Form $form) {
            $id = $form->model()->id;
            $item = ProductList::where('id', $id)->first();
            if ($form->status == 10) {
                ProductList::where('id', $id)->update(['send_time' => time()]);

                $lists = ProductList::where('order_id', $item->order_id)->get();
                $num = ProductList::where('order_id', $item->order_id)->count();
                if ($lists) {
                    foreach ($lists as $v) {
                        if ($v->status >= 10) {
                            $num = $num - 1;
                        }
                    }
                }

                if ($num == 0) {
                    ProductOrder::where('id', $item->order_id)->update(['status' => 10, 'send_time' => time()]);
                }

            } elseif ($form->status == 20) {
                ProductList::where('id', $id)->update(['finish_time' => time()]);
            }
        });

        $form->tools(function (Form\Tools $tools) {

            // 去掉`列表`按钮
            $tools->disableList();

            // 去掉`删除`按钮
            $tools->disableDelete();

            // 去掉`查看`按钮
            $tools->disableView();

        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });

        return $form;
    }
}